<template>
  <div class="permission-debug" v-if="showDebug">
    <div class="debug-header">
      <span>权限调试信息</span>
      <el-button type="text" @click="showDebug = false">关闭</el-button>
    </div>
    <div class="debug-content">
      <div class="debug-item">
        <span class="label">当前角色:</span>
        <span class="value">{{ roles.join(', ') }}</span>
      </div>
      <div class="debug-item">
        <span class="label">用户名:</span>
        <span class="value">{{ name }}</span>
      </div>
      <div class="debug-item">
        <span class="label">权限列表:</span>
        <div class="permission-list">
          <el-tag 
            v-for="(perm, index) in permissions" 
            :key="index" 
            size="mini"
            :type="perm === '*' ? 'success' : ''"
            effect="plain"
            style="margin: 2px"
          >
            {{ perm }}
          </el-tag>
        </div>
      </div>
      <div class="debug-item">
        <span class="label">权限测试:</span>
        <div class="permission-test">
          <div class="test-item" v-for="perm in testPermissions" :key="perm">
            <span>{{ perm }}:</span>
            <el-tag 
              size="mini" 
              :type="hasPermission(perm) ? 'success' : 'danger'"
            >
              {{ hasPermission(perm) ? '有权限' : '无权限' }}
            </el-tag>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { hasPermission } from '@/utils/permission'

export default {
  name: 'PermissionDebug',
  data() {
    return {
      showDebug: true,
      testPermissions: [
        'user:add',
        'user:edit',
        'user:delete',
        'user:status',
        'user:export',
        'user:assign'
      ]
    }
  },
  computed: {
    ...mapGetters({
      roles: 'user/roles',
      name: 'user/name',
      permissions: 'user/permissions'
    })
  },
  methods: {
    hasPermission
  }
}
</script>

<style lang="scss" scoped>
.permission-debug {
  position: fixed;
  right: 20px;
  bottom: 20px;
  width: 350px;
  background: #fff;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  z-index: 9999;
  
  .debug-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 15px;
    border-bottom: 1px solid #ebeef5;
    font-weight: bold;
    background-color: #f5f7fa;
  }
  
  .debug-content {
    padding: 15px;
    max-height: 400px;
    overflow-y: auto;
    
    .debug-item {
      margin-bottom: 10px;
      
      .label {
        font-weight: bold;
        display: block;
        margin-bottom: 5px;
      }
      
      .permission-list {
        display: flex;
        flex-wrap: wrap;
        margin-top: 5px;
      }
      
      .permission-test {
        display: flex;
        flex-direction: column;
        margin-top: 5px;
        
        .test-item {
          display: flex;
          justify-content: space-between;
          padding: 5px 0;
          border-bottom: 1px dashed #ebeef5;
          
          &:last-child {
            border-bottom: none;
          }
        }
      }
    }
  }
}
</style> 